package com.pucheng.bracelet.viewmodel.bracelet

import android.app.Activity
import android.content.Intent
import android.provider.MediaStore
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.clj.fastble.exception.BleException
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ble.BleCmd
import com.pucheng.bracelet.app.ble.BleWriteAndNotifyCallback
import com.pucheng.bracelet.app.ble.ClientManager
import com.pucheng.bracelet.app.util.ByteUtils
import com.pucheng.bracelet.ui.adapter.MenuData
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class BraceletViewModel : BaseViewModel() {
    val batteryStatus = MutableLiveData<String>().apply {
        value = "100%"
    }
    val menuDatas =
        MutableLiveData<MutableList<MenuData>>().apply {
            value = mutableListOf<MenuData>().apply {
//                add(MenuData(R.mipmap.yuzhiyibiaopan, "表盘选择"))
                add(MenuData(R.mipmap.message, "消息推送"))
                add(MenuData(R.mipmap.camera, "拍照控制"))
                add(MenuData(R.mipmap.naozhong, "日常闹钟"))
                add(MenuData(R.mipmap.setting, "其他设置"))
                add(MenuData(R.mipmap.upgrade, "固件升级"))
            }
        }

    val isStartCamera = MutableLiveData<Boolean>().apply { value = false }

    fun changeWatch(isBind: Boolean = false) {
        val datas = menuDatas.value
        menuDatas.value = datas?.also {
            if (it[0].name == "表盘选择") {
                if (!isBind){
                    it.removeAt(0)
                }

            } else {
                if (isBind)
                    it.add(0, MenuData(R.mipmap.yuzhiyibiaopan, "表盘选择"))
            }
        }

    }

    fun startCamera(){
        ClientManager.getInstance().sendControlCmd(BleCmd.START_CAMERA,object : BleWriteAndNotifyCallback(){
            override fun onCharacteristicChanged(data: ByteArray) {
                val isCamera = data[0] == ByteUtils.intToByte(0xFB) && data[1] == ByteUtils.intToByte(0x05)
                if (!isCamera){
                    ToastUtils.showShort("启动相机拍照失败")
                }
                isStartCamera.value = isCamera
            }

            override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
            }

            override fun onWriteFailure(exception: BleException?) {
                isStartCamera.value = false
                LogUtils.e("启动相机失败："+exception.toString())
                ToastUtils.showShort("启动相机失败："+exception!!.description)
            }
        })
    }


}
